{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Solving linear equations by different methods\n", "\n", "**Randall Romero Aguilar, PhD**\n", "\n", "This demo is based on the original Matlab demo accompanying the Computational Economics and Finance 2001 textbook by Mario Miranda and Paul Fackler.\n", "\n", "Original (Matlab) CompEcon file: **demlin01.m**\n", "\n", "Running this file requires the Python version of CompEcon. This can be installed with pip by running\n", "\n", " !pip install compecon --upgrade\n", "\n", "Last updated: 2022-Ago-07\n", "
\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from numpy.linalg import solve, inv\n", "from timeit import default_timer as timer\n", "from numba import jit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make a function to time " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "tic = lambda: timer()\n", "toc = lambda t: 1000* (timer() - t) # ellapsed milliseconds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Milliseconds required to solve n by n linear equation $Ax = b$\n", "m times using solve(A, b) and dot(inv(A), b), computing inverse only once." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "mvalues = [1, 100]\n", "nvalues = [50, 500]\n", "\n", "cases = pd.MultiIndex.from_product([mvalues, nvalues], names=['m','n'])\n", "results0 = pd.DataFrame(index=cases, columns=['solve(A,b)', 'inv(A) @ b'])\n", "\n", "for m, n in cases:\n", " A = np.random.rand(n, n)\n", " b = np.random.rand(n, 1)\n", "\n", " tt = tic()\n", " for j in range(m):\n", " x = solve(A, b)\n", "\n", " results0.loc[(m, n), 'solve(A,b)'] = toc(tt)\n", "\n", " tt = tic()\n", " Ainv = inv(A)\n", " for j in range(m):\n", " x = Ainv @ b\n", "\n", " results0.loc[(m, n), 'inv(A) @ b'] = toc(tt)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "@jit\n", "def using_solve(A, b, m):\n", " for j in range(m):\n", " x = solve(A, b)\n", "\n", "@jit\n", "def using_inv(Ainv, b, m): \n", " for j in range(m):\n", " x = Ainv @ b\n", "\n", "#run once to compile\n", "using_solve(A, b, m)\n", "using_inv(Ainv, b, m)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "results1 = pd.DataFrame(index=cases, columns=['solve(A,b)', 'inv(A) @ b'])\n", "\n", "for m, n in cases:\n", " A = np.random.rand(n, n)\n", " b = np.random.rand(n, 1)\n", "\n", " tt = tic()\n", " using_solve(A, b, m)\n", "\n", " results1.loc[(m, n), 'solve(A,b)'] = toc(tt)\n", "\n", " tt = tic()\n", " Ainv = inv(A)\n", " using_inv(Ainv, b, m)\n", "\n", " results1.loc[(m, n), 'inv(A) @ b'] = toc(tt)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
  without jitusing jit
  solve(A,b)inv(A) @ bsolve(A,b)inv(A) @ b
mn    
1508.3245000.8778000.1047000.335600
5004.5228008.5745004.3241009.878600
100507.0594004.2322004.5383000.424400
500496.13610015.551200382.74600012.598900
\n" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([results0, results1], keys=['without jit', 'using jit'], axis=1).style.highlight_min(axis=1)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 ('base')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" }, "vscode": { "interpreter": { "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186" } } }, "nbformat": 4, "nbformat_minor": 0 }